﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace digitaling_image
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.textBox1.Text = "x + y";
        }

        private double z(double x, double y)
        {
            return x + y;
        }

        private double z1(double x, double y)
        {
            return x + y;
        }

        private double z2(double x, double y)
        {
            return x*x + y*y;
        }

        private double z3(double x, double y)
        {
            return x + y*y;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Drawing.Bitmap funImage = new System.Drawing.Bitmap(640, 480);

            double maxZ = -9999, minZ = 9999;
            double zCoodr;
            for (double x = -32; x <= 32; x += 0.1)
                for (double y = -24; y <= 24; y += 0.1)
                {
                    zCoodr = z(x, y);
                    if (zCoodr > maxZ)
                        maxZ = zCoodr;
                    if (zCoodr < minZ)
                        minZ = zCoodr;
                }
            maxZ += 10;
            minZ -= 10;
            double REZ = maxZ - minZ;

            label2.Text = "Min: " + minZ.ToString() + "; Max: " + maxZ.ToString() + "; REZ: " + REZ.ToString() + ";";

            int imageValue;
            for (double x = -32; x <= 32; x += 0.1)
                for (double y = -24; y <= 24; y += 0.1)
                {
                    zCoodr = z(x, y);
                    //imageValue = (int)((255 * zCoodr) / REZ);
                    //double value = ((zCoodr - minZ) * 255) / REZ;
                    imageValue = (int)(((zCoodr - minZ) * 256) / REZ);

                    int imx = (int)(x*10) + 320;
                    int imy = (int)(y*10) + 240;
                    funImage.SetPixel(imx, imy, Color.FromArgb(imageValue, imageValue, imageValue));
                }

            System.Drawing.Image funImage2 = Image.FromHbitmap(funImage.GetHbitmap());
            pictureBox1.CreateGraphics().DrawImage(funImage2, 0, 0);
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.tabControl2.SelectedIndex = this.tabControl1.SelectedIndex;
        }

        private void tabControl2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.tabControl1.SelectedIndex = this.tabControl2.SelectedIndex;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Drawing.Bitmap funImage = new System.Drawing.Bitmap(640, 480);

            double maxZ1 = -9999, minZ1 = 9999;
            double maxZ2 = -9999, minZ2 = 9999;
            double maxZ3 = -9999, minZ3 = 9999;
            double zCoodr;
            for (double x = -32; x <= 32; x += 0.1)
                for (double y = -24; y <= 24; y += 0.1)
                {
                    zCoodr = z1(x, y);
                    if (zCoodr > maxZ1)
                        maxZ1 = zCoodr;
                    if (zCoodr < minZ1)
                        minZ1 = zCoodr;

                    zCoodr = z2(x, y);
                    if (zCoodr > maxZ2)
                        maxZ2 = zCoodr;
                    if (zCoodr < minZ2)
                        minZ2 = zCoodr;

                    zCoodr = z3(x, y);
                    if (zCoodr > maxZ3)
                        maxZ3 = zCoodr;
                    if (zCoodr < minZ3)
                        minZ3 = zCoodr;
                }
            maxZ1 += 10;
            minZ1 -= 10;

            maxZ2 += 10;
            minZ2 -= 10;

            maxZ3 += 10;
            minZ3 -= 10;

            double REZ1 = maxZ1 - minZ1;
            double REZ2 = maxZ2 - minZ2;
            double REZ3 = maxZ3 - minZ3;

            label8.Text = "Min: " + minZ1.ToString() + "; Max: " + maxZ1.ToString() + "; REZ: " + REZ1.ToString() + ";";
            label9.Text = "Min: " + minZ2.ToString() + "; Max: " + maxZ2.ToString() + "; REZ: " + REZ2.ToString() + ";";
            label10.Text = "Min: " + minZ3.ToString() + "; Max: " + maxZ3.ToString() + "; REZ: " + REZ3.ToString() + ";";

            int imageValueR, imageValueG, imageValueB;
            for (double x = -32; x <= 32; x += 0.1)
                for (double y = -24; y <= 24; y += 0.1)
                {
                    zCoodr = z1(x, y);
                    imageValueR = (int)(((zCoodr - minZ1) * 256) / REZ1);

                    zCoodr = z2(x, y);
                    imageValueG = (int)(((zCoodr - minZ2) * 256) / REZ2);

                    zCoodr = z3(x, y);
                    imageValueB = (int)(((zCoodr - minZ3) * 256) / REZ3);


                    int imx = (int)(x * 10) + 320;
                    int imy = (int)(y * 10) + 240;
                    funImage.SetPixel(imx, imy, Color.FromArgb(imageValueR, imageValueG, imageValueB));
                }

            System.Drawing.Image funImage2 = Image.FromHbitmap(funImage.GetHbitmap());
            pictureBox2.CreateGraphics().DrawImage(funImage2, 0, 0);
        }
    }
}
